package com.project.base.baseproject.AppCustoms.SelectImageActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bm.library.PhotoView;
import com.project.base.baseproject.AppCustoms.MyApplication.MyApplication;
import com.project.base.baseproject.R;
import com.project.base.baseproject.Utikles.DiviceInfo.DiviceInfo;
import com.project.base.baseproject.Utikles.ThirdParty.RunInMainThread.UIKit;
import com.squareup.picasso.Picasso;
import com.zxy.tiny.Tiny;
import com.zxy.tiny.callback.BitmapCallback;

import java.lang.ref.SoftReference;
import java.util.ArrayList;

/**
 * Created by mac on 2017/5/25.
 */
//多图选择数据源
public class MuliteSelectImageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    private ArrayList<BangBangPhoto> mData;
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    //初始化数据
    public MuliteSelectImageAdapter(ArrayList<BangBangPhoto> arrayList) {
        this.mData = arrayList;
    }
    //更新数据
    public void updateData(ArrayList<BangBangPhoto> data) {
        this.mData = data;
        notifyDataSetChanged();
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.mulite_select_image_item_layout, parent, false);
        // 实例化viewholder
        ViewHolder viewHolder = new ViewHolder(v);
        v.setOnClickListener(this);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final ViewHolder holder1 = (ViewHolder) holder;
        //取得地址
        final BangBangPhoto bangBangPhoto = this.mData.get(position);
        Tiny.BitmapCompressOptions options = new Tiny.BitmapCompressOptions();
        options.width = DiviceInfo.diviceWidth(MyApplication.getInstance()) / 3;
        options.height = options.width;
        if(bangBangPhoto.getOiginalImage() != null) {
            holder1.imageView.setImageBitmap(bangBangPhoto.getOiginalImage());
        } else {
            Tiny.getInstance().source(bangBangPhoto.getLocalPath()).asBitmap().withOptions(options).compress(new BitmapCallback() {
                @Override
                public void callback(boolean isSuccess, final Bitmap bitmap) {
                    bangBangPhoto.setOiginalImage(bitmap);
                    UIKit.runOnMainThreadAsync(new Runnable() {
                        @Override
                        public void run() {
                            holder1.imageView.setImageBitmap(bitmap);
                        }
                    });
                }
            });
        }
        //设置是否被选中
        if(bangBangPhoto.isSelected() == true) {
            holder1.selectImage.setImageResource(R.drawable.pictures_selected);
        } else {
            holder1.selectImage.setImageResource(R.drawable.picture_unselected);
        }
        //设置高度
        ViewGroup.LayoutParams layoutParams = holder1.imageView.getLayoutParams();
        layoutParams.height = DiviceInfo.diviceWidth(MyApplication.getInstance()) / 3;
        layoutParams.width = DiviceInfo.diviceWidth(MyApplication.getInstance()) / 3;
        holder1.imageView.setLayoutParams(layoutParams);
        holder1.itemView.setTag(bangBangPhoto);
    }
    @Override
    public int getItemCount() {
        return this.mData.size();
    }

    @Override
    public void onClick(View view) {
        if(mOnItemClickListener != null)
            mOnItemClickListener.imageClicked(view,(BangBangPhoto)view.getTag());
    }
    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        ImageView selectImage;
        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.mulite_select_image);
            selectImage = (ImageView) itemView.findViewById(R.id.mulite_select);
        }
    }
    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
    //define interface
    public static interface OnRecyclerViewItemClickListener {
        void imageClicked(View view,BangBangPhoto bangBangPhoto);
    }
}
